$(function() {
	laydate.render({
	    elem: '#year',
	    trigger: 'click',
	    type: 'year',
	    done: function(value, date) {
	        $('#year').val(value);
	    }
	})
	laydate.render({
	    elem: '#submit_year',
	    trigger: 'click',
	    type: 'year',
	    done: function(value, date) {
	        $('#submit_year').val(value);
	    }
	})

  $('#name').searchableSelect();

	$('#search').click(function(event) {
		search();
	});

	$("#reset").click(function(event) {
		$("#name").val('-1');
		$(".searchable-select .searchable-select-holder").html('请选择成果人');
		$("#submit_year").val('');
		$("#year").val('');
		$("#type").val('-1');
		$("#information").hide();
		$("#result").html('');
	});
	$("#name").change(function(event) {
		search();
	});
	$("#type").change(function(event) {
		search();
	});
});

function search(){
	var name=$("#name").val();
	var year=$("#year").val();
	var submit_year=$("#submit_year").val();
	var type=$("#type").val();
	if(name=='-1'){
		window.parent.topMessagePrompt('error','请选择成果人');
		$("#result").html('');
		$("#information").html('');
		return;
	}
	$.post(getRootPath()+"/Admin/Results/personData", 
		{
			name:name,
			year:year,
			submit_year:submit_year,
			type:type
		}, 
		function(data) {
		if(data.status){
			var t=JSON.parse(data.v);

			// 个人信息
			html="<tbody><tr><td>"+t.user.username+"</td><td>"+t.user.birthday_char+"</td><td>"+t.user.max_xl+"</td><td>"+t.user.title+"</td><td>"+t.user.academic_background+"</td><td>"+t.user.dept_name+"</td></tr> </tbody>";
			$("#information table tbody").html('');
			$("#information table").append(html);
			$("#information").show();

			// 搜索结果
			$("#result").html('');
			$.each(t.data, function(k, v) {
				html1='<div class="col-xs-12"> <div class="page-header"> <h1>'+v.table_name+'</h1> </div><table  class="table  table-bordered table-hover "><thead> <tr>';
				if(v.data.length!=0){
					$.each(v.table_col, function(k1, v1) {
						html1+="<th style=\"width:350px;\"><div>"+v1+"</div></th>";
					});
					html1+=" </tr> </thead><tbody>";
					$.each(v.data, function(k1, v1) {
						html1+="<tr>";
						$.each(v1, function(k2, v2) {
							if(k2=='id'){
								html1+="<td class='center'> <label class='pos-rel'> <input type='checkbox' class='ace' name='check["+k+"][]' value='"+v2+"' checked> <span class='lbl'></span> </label> </td><td>"+(k1+1)+"</td>";
							 }else{
								if(k2 == 'only_code'){
									html1+="<td><div style=\"word-wrap: break-word;width:100px;\">"+v2+"</div></td>";
								}else{
									html1+="<td>"+v2+"</td>";
								}
							 }
						});
						html1+="<tr>";
					});
				}
				
				html1+="</tbody> </table></div>";
				$("#result").append(html1);
			});
			
		}
	},'json');
}

function exportData(){	
	var data=$("form").serialize();	
	var name=$("#name").val();
	if(name=='-1'){
		window.parent.topMessagePrompt('error','请选择成果人');
		return;
	}
	if(data.indexOf("check") == -1){
		window.parent.topMessagePrompt('error','没有选择导出的数据');
		return;
	}
	var load_alert=loadAlert('数据统计中...');
	$.ajax({
		url: '	Admin/Results/personDataExport',
		type: 'POST',
		dataType: 'json',
		data: data,
		success:function(data) {
			if(data.status){
				closeAlert(load_alert);
				window.location.href=data.v;
			}else{
				window.parent.topMessagePrompt('error',ret.msg);
			}
		}
	})
}